package com.test10_接口应用;

import org.junit.Test;

interface IGraphics {   //图形描述标准
    public void paint();
}

class Point {   //描述坐标点
    private double x;   // x坐标
    private double y;   // y坐标

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return this.x;
    }

    public double getY() {
        return this.y;
    }
}

class Circular implements IGraphics {   // 定义圆形
    private Point center;   // 中心坐标
    private double radius;  // 圆形半径

    public Circular(Point center, double radius) {
        this.center = center;
        this.radius = radius;
    }

    @Override
    public void paint() {
        System.out.println("【圆形】中心点坐标 (x=" + center.getX() + ", y=" + center.getY() + ")，半径=" + this.radius + ")");
    }
}

class Triangle implements IGraphics {   // 定义三角形
    private Point[] points;

    public Triangle(Point p1, Point p2, Point p3) {
        this.points = new Point[] {p1, p2, p3};
    }

    @Override
    public void paint() {
        System.out.println("【三角形】第一条边 (x=" + this.points[0].getX() + ", y=" + this.points[0].getY() + ")");
        System.out.println("【三角形】第二条边 (x=" + this.points[1].getX() + ", y=" + this.points[1].getY() + ")");
        System.out.println("【三角形】第三条边 (x=" + this.points[2].getX() + ", y=" + this.points[2].getY() + ")");
    }
}

/**
 * 图形工厂
 */
class GraphicsFactory {
    public static IGraphics getInstance(String type, double... args) {
        if ("Circular".equalsIgnoreCase(type)) {
            return new Circular(new Point(args[0], args[1]), args[2]);
        } else if ("Triangle".equalsIgnoreCase(type)) {
            return new Triangle(new Point(args[0], args[1]), new Point(args[2], args[3]), new Point(args[4], args[5]));
        }
        return null;
    }
}

public class YootkDemo_02 {

    @Test
    public void test1() {
        {
            print(GraphicsFactory.getInstance("Circular", 1.0, 2.0, 3.0));
        }
        {
            print(GraphicsFactory.getInstance("Triangle", 1.0, 2.0, 3.0, 4.0, 5.0, 6.0));
        }
    }

    public static void print(IGraphics graphics) {
        graphics.paint();
    }
}
